package com.gxust.controller;

import com.gxust.pojo.Cart;
import com.gxust.pojo.PageBean;
import com.gxust.util.jwt.GetData;
import com.gxust.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 购物车管理
 */
@Slf4j
@RestController
@Api(tags = "购物车管理")
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private com.gxust.service.CartService CartService;

    /**
     * 添加购物车
     */
    @PostMapping("/add")
    @ApiOperation("新增购物车信息")
    public Result add(HttpServletRequest request,@RequestBody Cart cart){
        log.info("新增购物车信息：{}", cart);
        //        调用service函数
        Integer userId = GetData.getUserId(request);
        cart.setUserId(userId);
        CartService.add(cart);
        return Result.success();
    }

    /**
     * 修改购物车某商品数量
     */
    @PostMapping("/update")
    @ApiOperation("修改购物车某商品数量")
    public Result update(HttpServletRequest request,@RequestBody Cart cart){
        log.info("新增购物车信息：{}", cart);
        Integer userId = GetData.getUserId(request);
        cart.setUserId(userId);
        CartService.update(cart);
        return Result.success();

    }

    /**
     * 购物车信息
     */
    @GetMapping("/info")
    @ApiOperation("获取购物车")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       HttpServletRequest request){
        log.info("分页查询, 参数: {},{}",page,pageSize);
        //调用service分页查询
        Integer userId = GetData.getUserId(request);
//        Integer userId = BaseContext.getCurrentId().intValue();
        PageBean pageBean = CartService.page(page,pageSize,userId);
        return Result.success(pageBean);
    }

    /**
     * 删除购物车信息
     */
    @PostMapping("/delete")
    @ApiOperation("删除购物车商品")
    public Result cartDel(@RequestBody Map<String, Object> request) {
        Integer cartId  = (Integer) request.get("CartID");
        log.info("查询商品详细信息");
        // 调用 service 函数
         CartService.del(cartId);
        return Result.success();
    }

    /**
     * 根据id查找商品
     */
    @PostMapping("/getCartById")
    @ApiOperation("根据id获取商品信息")
    public Result CartInfoById(@RequestBody Map<String, Object> request,HttpServletRequest request1) {
        // 获取用户ID
        Integer userId = GetData.getUserId(request1);

        Object numericIdArrayObject = request.get("numericIdArray");
        if (numericIdArrayObject instanceof List<?>) {
            List<Integer> numericIdList = (List<Integer>) numericIdArrayObject;

            if (!numericIdList.isEmpty()) {
                List<Map<String, Object>> productList = new ArrayList<>();
                for (Integer productId : numericIdList) {
                    Map<String, Object> productInfo = CartService.CartInfoById(userId, productId);
                    productList.add(productInfo);
                }
                return Result.success(productList);
            } else {
                return Result.error("numericIdArray 参数不能为空");
            }
        } else {
            return Result.error("numericIdArray 参数类型错误");
        }
    }


}

